home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10304 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: garden.csc.calpoly.edu!not-for-mail
  2. From: dstubbs@garden.csc.calpoly.edu (Dan Stubbs)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: while loop problem
  5. Date: 16 Mar 1996 09:27:02 -0800
  6. Organization: Cal Poly, San Luis Obispo
  7. Message-ID: <4ietl6$pqm@garden.csc.calpoly.edu>
  8. References: <Pine.OSF.3.91.960312133449.7844B-100000@io.UWinnipeg.ca> <Do6DB0.EtE@microunity.com> <Pine.A32.3.91.960312171258.149477C-100000@black.weeg.uiowa.edu> <DoBAC8.K3o@iquest.net>
  9. NNTP-Posting-User: dstubbs@garden.csc.calpoly.edu
  10.  
  11. In article <DoBAC8.K3o@iquest.net>, Doug Miller <dlmiller@iquest.net> wrote:
  12. >The Amorphous Mass <robinson@blue.weeg.uiowa.edu> wrote:
  13. >
  14. >+On Tue, 12 Mar 1996, Tom Sanders wrote:
  15. >+
  16. >+> Bill Simpson <wsimpson@uwinnipeg.ca> writes:
  17. >+> |> Consider the following code fragment:
  18. >+> |>
  19. >+> |> y=2000; z=1000;
  20. >+> |> x=0;
  21. >+> |> while(x<=XMAX)
  22. >+> |>     {
  23. >+> |>     x+=(int) erand(mean);
  24. >+> |>     setdot(x,y,z);
  25. >+> |>     }
  26. >+> |>
  27. >+> |> The above code will also attempt to plot one point at an x value >XMAX
  28. >+> |> before it terminates.
  29. >+> |>
  30. >+> |> Is there a nice way to write the code so it works properly?
  31. >+>
  32. >
  33. >Well, let's give him a correct answer.
  34. >
  35. >In the *original* loop, just change the test (x <= XMAX) to (x < XMAX).
  36. >There.  That wasn't so hard, was it?
  37. >
  38.  
  39. No, it wasn't so hard, and it also is not correct. The problem is 
  40. updating the value of x and then writing it before testing to make
  41. sure the new x value is acceptable. Several posters have given correct
  42. solutions. Here is one of them again.
  43.  
  44.        y=2000; z=1000;
  45.        x+=(int) erand(mean);
  46.        while (x <= XMAX)
  47.        {
  48.           setdot(x,y,z);
  49.           x+=(int) erand(mean);
  50.        }
  51.  
  52.